//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    //城市
    bdcity:'',
    //轮播图
    imgUrls: [],
    indicatorDots: true,  //是否显示面板指示点
    autoplay: true,  //是否自动切换
    interval: 4000,  //自动切换时间间隔
    circular: true,  //是否采用衔接滑动
    //end
    //分类
    tcaty:[],
    show:false,
    MaterialList:[]
  },

  /**
    * 生命周期函数--监听页面加载
    */
  onLoad: function (o) {
    this.getCarouselPic();
    this.getMaterial();
    this.getgg();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  //公告
  onShow: function (e) {
    var that = this
    var userInfo = wx.getStorageSync("userInfo");
    if (userInfo){
      this.setData({
        user: userInfo,
        uid :userInfo.id,
      })
    }
      app.setCity(function (res) {
        if (res != false) {
          that.setData({
            bdcity: res,
            show:true
          })
          
          // //tab切换
          // app.wxRequest('get', '/Samecity/class/classification', {}, function (res) {
          //   if (res.data.code == 1) {
          //     that.setData({
          //       shangClassif: res.data.data
          //     })
          //   }
          // }, function (err) {
          // })
          // //同城服务分类
          // app.wxRequest('get', '/Samecity/class/classification', { id: 1 }, function (res) {
          //   if (res.data.code == 1) {
          //     that.setData({
          //       tcaty: res.data.data
          //     })
          //   } else {
          //     wx.showToast({
          //       title: res.data.msg,
          //       icon: 'none'
          //     })
          //   }
          // }, function (err) {
          //   wx.showToast({
          //     title: err.data.msg,
          //     icon: 'none'
          //   })
          // })
        }
      })
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

 /*收藏*/
 collection: function (e) {

  let that = this

  var key=e.currentTarget.dataset['index'];
  var uid = that.data.uid
  var MaterialList=that.data.MaterialList
  var id = MaterialList[key].id
  var type = 1
  console.log(e)
  app.wxRequest('get','/Samecity/Ads/getsc', {id:id,type:type,uid:uid}, function (res) {
    if(res.data.code==1){
      if(MaterialList[key].collection==true){//执行取消操作
        MaterialList[key].collection=false
      }else{//执行收藏操作
        MaterialList[key].collection=true
      }
      that.setData({
        MaterialList:MaterialList
      })
      console.log(that.data.MaterialList);
      wx.showToast({
        title: res.data.msg,
        icon: 'none',
        duration: 2000
      })
    
    }else{
      wx.showToast({
        title: res.data.msg,
        icon: 'none',
        duration: 2000
      })
    }
 
  })
},

/*拨号*/
  calling: function (e) {
    let that=this
    var key = e.currentTarget.dataset['index'];
    var MaterialList = that.data.MaterialList
    var tel = MaterialList[key].tel
    wx.showActionSheet({
      itemList: [tel],
      success: function (res) {
        console.log(res) //当点击400-900-2250就相当于点击了
        wx.makePhoneCall({
          phoneNumber: tel, //此号码并非真实电话号码，仅用于测试  
          success: function () {
            console.log("拨打电话成功！")
          },
          fail: function () {
            console.log("拨打电话失败！")
          }
        })
        if (!res.cancel) {
          console.log(res.tapIndex)
        }
      }
    });
  },
  //获取首页轮播
  getCarouselPic:function(){
    let _this=this;
    app.wxRequest('get','/Samecity/Ads/getCarouselPic', {}, function (res) {
      console.log(res);
       _this.setData({
         imgUrls: res.data.data
       })
    }, function (err) {
      wx.showToast({
        title: err.data.msg,
        icon: 'none'
      })
    })
  },

  //获取公告
  getgg:function(){
    let _this=this;
    app.wxRequest('get','/Samecity/Ads/getAnnouncement', {}, function (res) {
      console.log(res);
       _this.setData({
         list: res.data.data
       })
    }, function (err) {
      wx.showToast({
        title: err.data.msg,
        icon: 'none'
      })
    })
  },
  
  //信息推荐
  getMaterial:function(){
    let _this=this
    var city = wx.getStorageSync("city")
    var uid = _this.data.uid
    app.wxRequest('get','/Samecity/Ads/getMaterial', {city:city,uid:uid}, function (res) {
      console.log(res)
      _this.setData({
        MaterialList: res.data.data,
      })
    }, function (err) {
      wx.showToast({
        title: err.data.msg,
        icon: 'none'
      })
    })
  }
})
